package com.yanqu.road.logcollector.timer;

import com.yanqu.road.utils.executor.GeneralTask;
import com.yanqu.road.utils.executor.ScheduledThreadPoolHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.concurrent.ScheduledExecutorService;

/**
 * 所有的定时器写在这边
 */
public class GeneralAgentTimerMgr {

    private static final Logger log = LogManager.getLogger(GeneralAgentTimerMgr.class.getName());

    private static ScheduledExecutorService timerExecutorService;

    public static synchronized boolean initGlobalTimers() {
        if (timerExecutorService == null) {
            int corePoolSize = Runtime.getRuntime().availableProcessors() * 2;
            corePoolSize = Math.min(8, Math.max(4, corePoolSize));
            timerExecutorService = ScheduledThreadPoolHelper.createGameScheduledThreadPoolExecutor(corePoolSize, "timer-gameServer");
        }

        return true;
    }

    public static ScheduledExecutorService getTimerExecutorService() {
        return timerExecutorService;
    }

    private static int getNextResetTaskExecuteTime() {
        LocalDateTime currentTime = LocalDateTime.now();
        LocalDateTime nextExecute = currentTime.plusDays(1);
        nextExecute = LocalDateTime.of(nextExecute.getYear(), nextExecute.getMonth(), nextExecute.getDayOfMonth(), 0, 0,
                0, 0);
        return (int) (Duration.between(currentTime, nextExecute).getSeconds());
    }

    public static void stop() {
    }

    private static void cancelTask(GeneralTask task) {
        if (null != task) {
            task.cancel(false);
        }
    }
}
